class Event < Core
  has_one :core, :as => :resource  
  
  belongs_to :place
  belongs_to :user
  
  has_many :forum_messages

  belongs_to :festival, :class_name => 'Event', :foreign_key => 'festival_id'
  
  has_and_belongs_to_many :humans, :foreign_key => 'event_id', :association_foreign_key => 'human_id'

  validates_presence_of :user, :date, :date_end, :place_id
  
  default_value_for :date do
    Date.today
  end
  default_value_for :date_end do
    Date.today
  end
  
     
  default_value_for :part_of_day, '1111'
  
  default_value_for :is_cancelled, false
  default_value_for :with_pass, false
  
  before_validation { | record | 
    record.set_colors    
    
  }
  
  def place_attributes=(place)    
    if not place[:id].blank?
      self.place = Place.find(:first, :conditions => ["id = ? ", place[:id]])
    elsif (place[:is_generic].to_s == "1"  and  not place[:location_name].blank?)
      loc = Location.set_location(:full_address => place[:location_name])
      unless (p = Place.find(:first, :conditions => ["is_generic = ? and name = ?", 1, loc.full_address]))
        p = Place.new(:name => loc.full_address, :is_generic => 1, :location => loc ) if loc
        p.save
      end
      self.place = p
    elsif (not place[:name].blank? and not place[:location_name].blank?)
      loc = Location.set_location(:full_address => place[:location_name])
      unless (p = Place.find(:first, :conditions => ["name = ? and location_id = ?", place[:name], loc.id]))
        p = Place.new(:name => place[:name], :location => loc )
        p.save
      end
      self.place = p
    end    
  end
  
  def images_view
    a = []
    a << { :big => get_avatar(:thumb_big), :original => get_avatar(:original) }    
    a << { :big => place.get_avatar(:thumb_big), :original => place.get_avatar(:original) } if place.image
    for i in humans 
      a << { :big => i.get_avatar(:thumb_big), :original => i.get_avatar(:original) } if i.image
    end
    return a
  end
  
  def set_colors
    c = []
    kinds.each do |i|      
      c << i.color  
   end
   self.colors = c.join(",")
  end
   
end
